$(document).ready(function () {
    $.ajax({
        /*url: "./json/users/info.json",*/
        url: "http://"+urla+"/LoginStatusServlet",
        type: "GET",
        dataType: 'JSON',
        success: function (result) {
            if (result.status==0){
                $("#username").text(result.username);
            }
            showOrderList();
        }
    });
    function showOrderList() {
        $.ajax({
            /*url: "./json/order/list.json!pageNum=1.json",*/
            url: "http://" + urla + "/GetUserOrderByTimeServlet",
            type: "GET",
            data:{
                "choosePage":1,
                "pageSize":5,
            },
            dataType: 'JSON',
            success: function (result) {
                // console.log(result);
                $.each(result, function (j, obj) {
                    var totalPrice=0;
                    var $table = $("<table class='table tb-item'></table>");
                    var $colgroup = $("<colgroup>" +
                        "<col class='col-lg-4'>" +
                        "<col class='col-lg-2'>" +
                        "<col class='col-lg-2'>" +
                        "<col class='col-lg-2'>" +
                        "<col class='col-lg-2'></colgroup>");
                    var $thead = $("<thead><tr>" +
                        "<td colspan='1'><span>&nbsp&nbsp" + obj.order_createTime + "&nbsp&nbsp</span></td>" +
                        "<td colspan='3'><span>订单编号：" + obj.order_id + "</span></td>" +
                        "<td colspan='1' style='text-align: right;font-size: 12px'>" +
                        "<a href='order_pay.html?orderNo=" + obj.order_id + "'>查看详情>></a></td></tr></thead>");
                    var $tbody = $("<tbody id='tb" + obj.order_id + "'></tbody>");
                    var orderNo = obj.order_id;
                    $table.append($colgroup, $thead, $tbody);
                    $(".panel-body").append($table);
                    console.log(obj);
                    var num = obj.good.length;
                    $.each(obj.good, function (x, t) {
                        var css = "";
                        if (obj.pay==false){
                            totalPrice = "未支付";
                            var css = "style=\"color: red;\"";
                        }else {
                            totalPrice = totalPrice + obj.good_amount[t.good_id]*t.good_price;
                        }

                        // console.log(totalPrice);
                        var $tr = $("<tr></tr>");
                        // console.log(x)
                        var mainUrl = t.good_url.split(",")[0];
                        var $td1 = $("<td>" + "<div class='product-info'>" + "<img src='"+mainUrl+"'>" + "<a href='item.html?id="+t.good_id+"'>" + t.good_name + "</a></div></td>");
                        var $td2 = $("<td class='td-price'>" + "<div>" + "<span>￥</span>" + "<span>" + t.good_price + "</span></div></td>");

                        var $td3 = $("<td class='td-num'>" + "<div>" + obj.good_amount[t.good_id] + "件</div></td>");
                        var $td4 = $("<td class='td-total' rowspan='" + num + "'>" + "<div "+css+">" + "<span>¥</span>" + "<span>" + totalPrice + "</span>" + "<p>(优惠：¥0.00)</p></div></td>");
                        var sendStatus = "";
                        if (obj.status){
                            sendStatus = "已发货";
                        }else {
                            sendStatus = "未发货";
                        }
                        var $td5 = $("<td class='td-status' rowspan='" + num + "'>" +
                            "<div>" + sendStatus + "</div></td>");
                        $("#tb" + orderNo).append($tr);
                        $tr.append($td1, $td2, $td3, $td4, $td5);
                    });


                    // if ($("#tb" + orderNo).children("tr").length > 1) {
                    //     $("#tb" + orderNo + " tr").not(":first").find($(".td-total")).remove();
                    //     $("#tb" + orderNo + " tr").not(":first").find($(".td-status")).remove();
                    // }
                });
            }
        });
    }

});